home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu017.dms / pu017.adf / Gadgets / Example9.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  232 lines

  1. /* Example9                                                           */
  2. /* This program will open a normal window which is connected to the   */
  3. /* Workbench Screen. The window will use all System Gadgets, and will */
  4. /* close first when the user has selected the System gadget Close     */
  5. /* window. Inside the window we have put a Proportional gadget.       */
  6.  
  7.  
  8.  
  9. #include <intuition/intuition.h>
  10.  
  11.  
  12.  
  13. struct IntuitionBase *IntuitionBase;
  14.  
  15.  
  16.  
  17. /* THE PROPORTIONAL GADGET's STRUCTURES: */
  18.  
  19. /* The IntuiText structure: */
  20. struct IntuiText my_text=
  21. {
  22.   1,         /* FrontPen, colour register 1. */
  23.   0,         /* BackPen, colour register 0. */
  24.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  25.              /* change the background. */ 
  26.   -65, 2,    /* LeftEdge, TopEdge. */
  27.   NULL,      /* ITextFont, use default font. */
  28.   "Volume:", /* IText, the text that will be printed. */
  29.   NULL,      /* NextText, no other IntuiText structures. */
  30. };
  31.  
  32.  
  33. /* We need to declare an Image structure for the knob, but since */
  34. /* Intuition will take care of the size etc of the knob, we do not need */
  35. /* to initialize the Image structure: */
  36. struct Image my_image;
  37.  
  38.  
  39. struct PropInfo my_prop_info=
  40. {
  41.   FREEHORIZ|      /* Flags, the knob should be moved horizontally, and */
  42.   AUTOKNOB,       /* Intuition should take care of the knob image. */
  43.   0,              /* HorizPot, start position of the knob. */
  44.   0,              /* VertPot, 0 since we will not move the knob hor. */
  45.   MAXBODY * 1/64, /* HorizBody, 64 steps. */
  46.   0,              /* VertBody, 0 since we will not move the knob hor. */
  47.  
  48.   /* These variables are initialized and maintained by Intuition: */
  49.  
  50.   0,              /* CWidth */
  51.   0,              /* CHeight */
  52.   0, 0,           /* HPotRes, VPotRes */
  53.   0,              /* LeftBorder */
  54.   0               /* TopBorder */
  55. };
  56.  
  57.  
  58. struct Gadget my_gadget=
  59. {
  60.   NULL,            /* NextGadget, no more gadgets in the list. */
  61.   80,              /* LeftEdge, 80 pixels out. */
  62.   30,              /* TopEdge, 30 lines down. */
  63.   200,             /* Width, 200 pixels wide. */
  64.   12,              /* Height, 12 pixels lines heigh. */
  65.   GADGHCOMP,       /* Flags, complement the colours. */
  66.   GADGIMMEDIATE|   /* Activation, our program will recieve a message */
  67.   RELVERIFY,       /* when the user has selected this gadget, and when */
  68.                    /* the user has released it. */ 
  69.   PROPGADGET,      /* GadgetType, a Proportional gadget. */
  70.   (APTR) &my_image,/* GadgetRender, a pointer to our Image structure. */
  71.                    /* (Intuition will take care of the knob image) */
  72.                    /* (See chapter 3 GRAPHICS for more information) */
  73.   NULL,            /* SelectRender, NULL since we do not supply the */
  74.                    /* gadget with an alternative image. */
  75.   &my_text,        /* GadgetText, volume. */
  76.   NULL,            /* MutualExclude, no mutual exclude. */
  77.   (APTR) &my_prop_info, /* SpecialInfo, pointer to a PropInfo structure. */
  78.   0,               /* GadgetID, no id. */
  79.   NULL             /* UserData, no user data connected to the gadget. */
  80. };
  81.  
  82.  
  83.  
  84. /* Declare a pointer to a Window structure: */ 
  85. struct Window *my_window;
  86.  
  87. /* Declare and initialize your NewWindow structure: */
  88. struct NewWindow my_new_window=
  89. {
  90.   50,            /* LeftEdge    x position of the window. */
  91.   25,            /* TopEdge     y positio of the window. */
  92.   320,           /* Width       320 pixels wide. */
  93.   100,           /* Height      100 lines high. */
  94.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  95.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  96.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  97.                  /*             user has selected the Close window gad, */
  98.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  99.   GADGETUP,      /*             a gadge has been released. */
  100.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  101.   WINDOWCLOSE|   /*             Close Gadget. */
  102.   WINDOWDRAG|    /*             Drag gadget. */
  103.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  104.   WINDOWSIZING|  /*             Sizing Gadget. */
  105.   ACTIVATE,      /*             The window should be Active when opened. */
  106.   &my_gadget,    /* FirstGadget A pointer to the String gadget. */
  107.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  108.   "Proportional Window", /* Title Title of the window. */
  109.   NULL,          /* Screen      Connected to the Workbench Screen. */
  110.   NULL,          /* BitMap      No Custom BitMap. */
  111.   320,           /* MinWidth    We will not allow the window to become */
  112.   50,            /* MinHeight   smaller than 320 x 50, and not bigger */
  113.   640,           /* MaxWidth    than 640 x 200. */
  114.   200,           /* MaxHeight */
  115.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  116. };
  117.  
  118.  
  119.  
  120. main()
  121. {
  122.   /* Boolean variable used for the while loop: */
  123.   BOOL close_me;
  124.  
  125.   /* Declare a variable in which we will store the IDCMP flag: */
  126.   ULONG class;
  127.  
  128.   /* Declare a pointer to an IntuiMessage structure: */
  129.   struct IntuiMessage *my_message;
  130.  
  131.  
  132.  
  133.   /* Before we can use Intuition we need to open the Intuition Library: */
  134.   IntuitionBase = (struct IntuitionBase *)
  135.     OpenLibrary( "intuition.library", 0 );
  136.   
  137.   if( IntuitionBase == NULL )
  138.     exit(); /* Could NOT open the Intuition Library! */
  139.  
  140.  
  141.  
  142.   /* We will now try to open the window: */
  143.   my_window = (struct Window *) OpenWindow( &my_new_window );
  144.   
  145.   /* Have we opened the window succesfully? */
  146.   if(my_window == NULL)
  147.   {
  148.     /* Could NOT open the Window! */
  149.     
  150.     /* Close the Intuition Library since we have opened it: */
  151.     CloseLibrary( IntuitionBase );
  152.  
  153.     exit();  
  154.   }
  155.  
  156.  
  157.  
  158.   /* We have opened the window, and everything seems to be OK. */
  159.  
  160.  
  161.  
  162.   close_me = FALSE;
  163.  
  164.   /* Stay in the while loop until the user has selected the Close window */
  165.   /* gadget: */
  166.   while( close_me == FALSE )
  167.   {
  168.     /* Wait until we have recieved a message: */
  169.     Wait( 1 << my_window->UserPort->mp_SigBit );
  170.  
  171.     /* We have now recieved one or more messages. */
  172.  
  173.     /* Since we may recieve several messages we stay in the while loop */
  174.     /* and collect, save, reply and execute the messages until there is */
  175.     /* a pause: */
  176.     while(my_message=(struct IntuiMessage *)GetMsg( my_window->UserPort))
  177.     {
  178.       /* GetMsg will return a pointer to a message if there was one, */
  179.       /* else it returns NULL. We will therefore stay in this while loop */
  180.       /* as long as there are some messages waiting in the port. */
  181.       
  182.       /* After we have collected the message we can read it, and save */
  183.       /* any important values which we maybe want to check later: */
  184.       class = my_message->Class;      /* Save the IDCMP flag. */
  185.  
  186.       /* After we have read it we reply as fast as possible: */
  187.       /* REMEMBER! Do never try to read a message after you have replied! */
  188.       /* Some other process has maybe changed it. */
  189.       ReplyMsg( my_message );
  190.  
  191.       /* Check which IDCMP flag was sent: */
  192.       switch( class )
  193.       {
  194.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  195.                close_me=TRUE;
  196.                break;
  197.              
  198.         case GADGETDOWN:   /* The user has selected the Prop. gadget: */
  199.                printf("Proportional gadget selected.\n");
  200.                break;
  201.              
  202.         case GADGETUP:     /* The user has released the Prop. gadget: */
  203.                printf("Proportional gadget released.\n");
  204.                break;
  205.       }
  206.     }
  207.     printf("Volume= %1.0f\n\n", (float) my_prop_info.HorizPot/MAXPOT*64);
  208.   }
  209.  
  210.   /* We should always close the windows we have opened before we leave: */
  211.   CloseWindow( my_window );
  212.  
  213.  
  214.  
  215.   /* Close the Intuition Library since we have opened it: */
  216.   CloseLibrary( IntuitionBase );
  217.  
  218.   /* THE END */
  219. }
  220.  
  221. /*************************************************************************/
  222. /* EXTRA INFORMATION:                                                    */
  223. /* We will recieve a message (GADGETDOWN) when the user selects the      */
  224. /* knob, and one message (GADGETUP) when the user releases the knob. If  */
  225. /* the user on the other hand clicks inside the container (not on the    */
  226. /* knob) we will recieve both a GADGETDOWN and a GADGETUP message at the */
  227. /* same time.                                                            */
  228. /* It is because of that we need to have a while loop which collects the */
  229. /* messages once one or more has arrived. We can not as before just wait */
  230. /* and then collect one message, since there may be more in the queue.   */
  231. /*************************************************************************/
  232.